<template>
  <el-form
    ref="userForm"
    :model="userInfo"
    :rules="rules"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:" prop="username">
      <el-input v-model="userInfo.username" style="width:300px" />
    </el-form-item>
    <el-form-item label="密码:" prop="password">
      <el-input v-model="userInfo.password" type="password" style="width:300px" />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="hSubmit">更新</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { upInfo } from '@/api/user'
import { putEmployees } from '@/api/employees'

export default {
  data() {
    return {
      userId: this.$route.query.userId,
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },

  created() {
    this.lodeInfo()
  },

  methods: {
    // 兜底校验
    hSubmit() {
      this.$refs.userForm.validate((valid) => {
        if (!valid) return // 校验失败
        // 校验成功
        this.lodeRole()
      })
    },
    // 修改
    async lodeRole() {
      try {
        const res = await putEmployees(this.userInfo)
        // console.log('putRole', res)
        // 保存数据
        if (res.code === 10000) {
          this.$message.success(res.message)
          this.$router.back()
        }
      } catch (err) {
        console.log('putRole', err)
      }
    },
    // 获取员工详细信息
    async lodeInfo() {
      try {
        const res = await upInfo(this.userId)
        // console.log('upInfo', res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.userInfo = res.data
        // 保存数据
      } catch (err) {
        console.log('upInfo', err)
      }
    }

  }
}
</script>
